package com.zlsy.secondtime.threadcoreknowledge.stopthreads;

/**
 * @author zhouliang
 * @date 2020/5/15 9:09
 * <p>
 * run方法内没有sleep或则wait方法时候，停止线程
 */
public class RightWayStopThreadWithoutSleep implements Runnable {

    @Override
    public void run() {
        int num = 0;
        while (num <= Integer.MAX_VALUE / 2) {
            if (num % 10000 == 0 && !Thread.currentThread().isInterrupted()) {
                System.out.println(num + "是10000的倍数.");
            }
            num++;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        RightWayStopThreadWithoutSleep r = new RightWayStopThreadWithoutSleep();
        Thread thread = new Thread(r);
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }
}
